home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / record.c < prev    next >
C/C++ Source or Header  |  1996-07-01  |  8KB  |  435 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include "../misc/misc.h"
  6. #include "dnsconf.h"
  7. #include "../netconf/netconf.h"
  8. #include "internal.h"
  9.  
  10. PUBLIC RECORD::RECORD(RECORD_TYPE _id)
  11. {
  12.     id = _id;
  13. }
  14.  
  15. /*
  16.     Tell if a record is of a certain type (A,SOA
  17. */
  18. PUBLIC bool RECORD::is(RECORD_TYPE _id)
  19. {
  20.     return id == _id;
  21. }
  22.  
  23. PUBLIC VIRTUAL void RECORD::sethostpart(const char *)  //hostpart
  24. {
  25.     fprintf (stderr,"Can't set host part for record type %d\n",id);
  26. }
  27. /*
  28.     Primary key comparison between record of the same type
  29.     This function is the default for record that are not compared.
  30. */
  31. PROTECTED VIRTUAL int RECORD::cmpsame(const RECORD *) //other
  32. {
  33.     assert (0);
  34.     return -1;
  35. }
  36. /*
  37.     Change the content of a record (The value, not the key) by
  38.     copying another record.
  39. */
  40. PUBLIC VIRTUAL int RECORD::set(const RECORD *)    //other
  41. {
  42.     assert (0);
  43.     return -1;
  44. }
  45.  
  46. /*
  47.     Compare two record.
  48.     Return -1 if they are not of the same type.
  49.     Return a strcmp() of their primary key if they are of the same type,
  50. */
  51. PUBLIC int RECORD::cmp(RECORD *other)
  52. {
  53.     int ret = -1;    
  54.     if (id == other->id){
  55.         ret = cmpsame(other);
  56.     }
  57.     return ret;
  58. }
  59.  
  60. /*
  61.     Compare the left value of a record with a string.
  62.     Return the output of strcmp().
  63. */
  64. PUBLIC VIRTUAL int RECORD::cmp_left(const char *)
  65. {
  66.     return -1;
  67. }
  68.  
  69. PUBLIC RECORD_IN::RECORD_IN(
  70.     const RECORD_PARSE &p,
  71.     RECORD_TYPE _id)
  72.     : RECORD (_id)
  73. {
  74.     ttl = p.ttl;
  75.     nottl = p.nottl;
  76.     ttlstr[0] = '\0';
  77.     if (!nottl) sprintf (ttlstr,"%ld",ttl);
  78. }
  79. PUBLIC RECORD_IN::RECORD_IN(RECORD_TYPE _id)
  80.     :RECORD (_id)
  81. {
  82.     ttl = 0;
  83.     nottl = true;
  84.     ttlstr[0] = '\0';
  85. }
  86.  
  87.  
  88. PUBLIC void RECORD::setcomment(const char *str)
  89. {
  90.     comment.setfrom (str);
  91. }
  92.  
  93. PUBLIC RECORD_COMMENT::RECORD_COMMENT (const char *str)
  94.     : RECORD (RTYPE_COMMENT)
  95. {
  96.     setcomment (str);
  97. }
  98.  
  99. PUBLIC void RECORD_COMMENT::print (FILE *fout) const
  100. {
  101.     fprintf (fout,"%s\n",comment.get());
  102. }
  103.  
  104.  
  105. PUBLIC RECORD_IN_A::RECORD_IN_A(const RECORD_PARSE &p)
  106.     : RECORD_IN(p,RTYPE_A)
  107. {
  108.     name.setfrom (p.f1);
  109.     addr.setfrom (p.f2);
  110. }
  111.  
  112. PUBLIC RECORD_IN_A::RECORD_IN_A(
  113.     const char *_name,
  114.     const char *_addr)
  115.     : RECORD_IN(RTYPE_A)
  116. {
  117.     name.setfrom(_name);
  118.     addr.setfrom(_addr);
  119. }
  120.  
  121. PUBLIC void RECORD_IN_A::sethostpart (const char *hostpart)
  122. {
  123.     name.setfrom (hostpart);
  124. }
  125.  
  126. PROTECTED int RECORD_IN_A::cmpsame(const RECORD *other)
  127. {
  128.     return name.icmp(((RECORD_IN_A*)other)->name);
  129. }
  130. PROTECTED int RECORD_IN_A::cmp_left(const char *str)
  131. {
  132.     return name.icmp(str);
  133. }
  134.  
  135. /*
  136.     Change the content of a record (The value, not the key) by
  137.     copying another record.
  138. */
  139. PUBLIC int RECORD_IN_A::set(const RECORD *other)
  140. {
  141.     addr.setfrom (((RECORD_IN_A*)other)->addr.get());
  142.     return 0;
  143. }
  144.  
  145. /*
  146.     Do a stricmp on the hostname
  147. */
  148. PUBLIC int RECORD_IN_A::cmphost(const char *host)
  149. {
  150.     return stricmp(host,name.get());
  151. }
  152.  
  153. PUBLIC void RECORD_IN_A::setip(const char *ip)
  154. {
  155.     addr.setfrom(ip);
  156. }
  157. PUBLIC void RECORD_IN_A::print (FILE *fout) const
  158. {
  159.     fprintf (fout,"%s\t%s\tIN\tA\t%s\n",name.get(),ttlstr,addr.get());
  160. }
  161.  
  162.  
  163. PUBLIC RECORD_IN_PTR::RECORD_IN_PTR(const RECORD_PARSE &p)
  164.     : RECORD_IN (p,RTYPE_PTR)
  165. {
  166.     name.setfrom (p.f2);
  167.     addr.setfrom (p.f1);
  168. }
  169. PUBLIC RECORD_IN_PTR::RECORD_IN_PTR(
  170.     const char *iprev,
  171.     const char *host)
  172.     : RECORD_IN(RTYPE_PTR)
  173. {
  174.     addr.setfrom(iprev);
  175.     name.setfrom(host);
  176.     dns_cnv2abs (name);
  177. }
  178. PROTECTED int RECORD_IN_PTR::cmpsame(const RECORD *other)
  179. {
  180.     return name.icmp(((RECORD_IN_PTR*)other)->name);
  181. }
  182. PROTECTED int RECORD_IN_PTR::cmp_left(const char *str)
  183. {
  184.     return addr.cmp(str);
  185. }
  186.  
  187. /*
  188.     Change the content of a record (The value, not the key) by
  189.     copying another record.
  190. */
  191. PUBLIC int RECORD_IN_PTR::set(const RECORD *other)
  192. {
  193.     name.setfrom (((RECORD_IN_PTR*)other)->name.get());
  194.     return 0;
  195. }
  196.     
  197. PUBLIC void RECORD_IN_PTR::sethostpart (const char *hostpart)
  198. {
  199.     addr.setfrom (hostpart);
  200. }
  201.  
  202. PUBLIC void RECORD_IN_PTR::print (FILE *fout) const
  203. {
  204.     fprintf (fout,"%s\t%s\tIN\tPTR\t%s\n",addr.get(),ttlstr,name.get());
  205. }
  206.  
  207. PUBLIC int RECORD_IN_PTR::cmpip(const char *ip)
  208. {
  209.     return addr.cmp(ip);
  210. }
  211.  
  212. PUBLIC RECORD_IN_NS::RECORD_IN_NS(const RECORD_PARSE &p)
  213.     : RECORD_IN(p,RTYPE_NS)
  214. {
  215.     name.setfrom (p.f1);
  216.     ns.setfrom (p.f2);
  217. }
  218.  
  219. PUBLIC RECORD_IN_NS::RECORD_IN_NS(const char *_name, const char *_ns)
  220.     : RECORD_IN(RTYPE_NS)
  221. {
  222.     name.setfrom (_name);
  223.     ns.setfrom (_ns);
  224.     dns_cnv2abs (ns);
  225. }
  226.  
  227. /*
  228.     Define the default NS record of a domain
  229. */
  230. PUBLIC RECORD_IN_NS::RECORD_IN_NS()
  231.     : RECORD_IN(RTYPE_NS)
  232. {
  233.     THISHOST thost;
  234.     name.setfrom ("@");
  235.     ns.setfrom(thost.getname1());
  236.     dns_cnv2abs (ns);
  237. }
  238.  
  239. PROTECTED int RECORD_IN_NS::cmpsame(const RECORD *other)
  240. {
  241.     return name.icmp(((RECORD_IN_NS*)other)->name);
  242. }
  243.  
  244. /*
  245.     Change the content of a record (The value, not the key) by
  246.     copying another record.
  247. */
  248. PUBLIC int RECORD_IN_NS::set(const RECORD *other)
  249. {
  250.     ns.setfrom (((RECORD_IN_NS*)other)->ns.get());
  251.     return 0;
  252. }
  253.  
  254. PUBLIC void RECORD_IN_NS::sethostpart (const char *hostpart)
  255. {
  256.     name.setfrom (hostpart);
  257. }
  258.  
  259. PUBLIC int RECORD_IN_NS::cmp_left (const char *str)
  260. {
  261.     return name.icmp(str);
  262. }
  263.  
  264. PUBLIC void RECORD_IN_NS::print (FILE *fout) const
  265. {
  266.     fprintf (fout,"%s\t%s\tIN\tNS\t%s\n",name.get(),ttlstr,ns.get());
  267. }
  268.  
  269. PUBLIC RECORD_IN_CNAME::RECORD_IN_CNAME(const RECORD_PARSE &p)
  270.     : RECORD_IN(p,RTYPE_CNAME)
  271. {
  272.     nickname.setfrom (p.f1);
  273.     name.setfrom (p.f2);
  274.     dns_cnv2abs (name);
  275. }
  276.  
  277. PUBLIC RECORD_IN_CNAME::RECORD_IN_CNAME(const char *cname, const char *realname)
  278.     : RECORD_IN(RTYPE_CNAME)
  279. {
  280.     nickname.setfrom (cname);
  281.     name.setfrom (realname);
  282.     dns_cnv2abs (name);
  283. }
  284.  
  285. PUBLIC int RECORD_IN_CNAME::cmp_left (const char *str)
  286. {
  287.     return nickname.cmp(str);
  288. }
  289.  
  290. PUBLIC void RECORD_IN_CNAME::sethostpart (const char *hostpart)
  291. {
  292.     nickname.setfrom (hostpart);
  293. }
  294. PROTECTED int RECORD_IN_CNAME::cmpsame(const RECORD *other)
  295. {
  296.     return nickname.cmp(((RECORD_IN_CNAME*)other)->nickname);
  297. }
  298.  
  299.  
  300. PUBLIC void RECORD_IN_CNAME::print (FILE *fout) const
  301. {
  302.     fprintf (fout,"%s\t%s\tIN\tCNAME\t%s\n",nickname.get(),ttlstr
  303.         ,name.get());
  304. }
  305.  
  306. PUBLIC RECORD_IN_MX::RECORD_IN_MX(const RECORD_PARSE &p)
  307.     : RECORD_IN(p,RTYPE_MX)
  308. {
  309.     mailname.setfrom (p.f1);
  310.     prefer = atoi(p.f2);
  311.     servname.setfrom (p.f3);
  312.     dns_cnv2abs (servname);
  313. }
  314. PUBLIC RECORD_IN_MX::RECORD_IN_MX(
  315.     const char *_mailname,
  316.     int priority,
  317.     const char *_servname)
  318.     : RECORD_IN(RTYPE_MX)
  319. {
  320.     mailname.setfrom (_mailname);
  321.     prefer = priority;
  322.     servname.setfrom (_servname);
  323.     dns_cnv2abs (servname);
  324. }
  325.  
  326. PROTECTED int RECORD_IN_MX::cmpsame(const RECORD *other)
  327. {
  328.     return mailname.cmp(((RECORD_IN_MX*)other)->mailname);
  329. }
  330.  
  331. /*
  332.     Change the content of a record (The value, not the key) by
  333.     copying another record.
  334. */
  335. PUBLIC int RECORD_IN_MX::set(const RECORD *other)
  336. {
  337.     RECORD_IN_MX *othermx = (RECORD_IN_MX*)other;
  338.     servname.setfrom(othermx->servname.get());
  339.     dns_cnv2abs (servname);
  340.     prefer = othermx->prefer;
  341.     return 0;
  342. }
  343.  
  344. PUBLIC int RECORD_IN_MX::cmp_left (const char *str)
  345. {
  346.     return mailname.cmp(str);
  347. }
  348. PUBLIC void RECORD_IN_MX::print (FILE *fout) const
  349. {
  350.     fprintf (fout,"%s\t%s\tIN\tMX\t%d\t%s\n",mailname.get(),ttlstr
  351.         ,prefer,servname.get());
  352. }
  353.  
  354. PUBLIC void RECORD_IN_MX::sethostpart (const char *hostpart)
  355. {
  356.     mailname.setfrom (hostpart);
  357. }
  358.  
  359.  
  360. PUBLIC RECORD_PARSE::RECORD_PARSE()
  361. {
  362.     ttl = 0;
  363.     nottl = true;
  364.     f1[0] = f2[0] = f3[0] = '\0';
  365. }
  366.  
  367. PUBLIC RECORD_INCLUDE::RECORD_INCLUDE (const char *_path)
  368.     : RECORD(RTYPE_INCLUDE)
  369. {
  370.     path.setfrom (_path);
  371. }
  372. PUBLIC void RECORD_INCLUDE::print(FILE *fout) const
  373. {
  374.     fprintf (fout,"$INCLUDE %s\n",path.get());
  375. }
  376. PUBLIC void RECORD_INCLUDE::edit()
  377. {
  378. }
  379.  
  380. PUBLIC RECORD_END_INCLUDE::RECORD_END_INCLUDE ()
  381.     : RECORD(RTYPE_END_INCLUDE)
  382. {
  383. }
  384. PUBLIC void RECORD_END_INCLUDE::print(FILE *) const
  385. {
  386. }
  387. PUBLIC void RECORD_END_INCLUDE::edit()
  388. {
  389. }
  390.  
  391.  
  392. PUBLIC RECORD *RECORDS::getitem(int no) const
  393. {
  394.     return (RECORD*)ARRAY::getitem(no);
  395. }
  396.  
  397. /*
  398.     Save the record in a file
  399. */
  400. PUBLIC int RECORDS::save (TBFILE &tbf) const
  401. {
  402.     int ret = -1;
  403.     for (int i=0; i<getnb(); i++){
  404.         RECORD *rec = getitem(i);
  405.         if (rec->is(RTYPE_INCLUDE)){
  406.             rec->print (tbf.cur);
  407.             tbf.fopen (((RECORD_INCLUDE*)rec)->path.get(),"w");
  408.         }else if (rec->is(RTYPE_END_INCLUDE)){
  409.             tbf.fclose();
  410.         }else{
  411.             getitem(i)->print (tbf.cur);
  412.         }
  413.     }
  414.     return ret;
  415. }
  416.  
  417.  
  418. #ifdef TEST
  419.  
  420. int main (int argc, char *argv[])
  421. {
  422.     malloc_err (10);
  423.     if (argc != 2){
  424.         fprintf (stderr,"File name\n");
  425.     }else{
  426.         RECORDS recs;
  427.         recs.read (argv[1]);
  428.         recs.save ("/tmp/toto");
  429.     }
  430.     return 0;
  431. }
  432.  
  433. #endif
  434.  
  435.